package com.junxian.springboot03.common;

import redis.clients.jedis.Jedis;

public class TestSet {

    public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.31.90", 6379);
        jedis.auth("123456");

        jedis.flushDB();
        System.out.println("==============向集合set(无序)中添加元素（不重复）==============");
        System.out.println(jedis.sadd("set", "s0", "s1", "s2", "s3", "s4", "s5"));
        System.out.println(jedis.sadd("set", "s6"));
        System.out.println("set集合的所有元素：" + jedis.smembers("set"));
        System.out.println("删除set中的s2元素：" +jedis.srem("set", "s2"));
        System.out.println("set集合的所有元素：" + jedis.smembers("set"));
        System.out.println("删除set中的s1,s3元素：" +jedis.srem("set", "s1", "s3"));
        System.out.println("set集合的所有元素：" + jedis.smembers("set"));
        System.out.println("随机移除set集合的所有元素：" + jedis.spop("set"));
        System.out.println("set集合的所有元素：" + jedis.smembers("set"));
        System.out.println("set集合的所有元素个数：" + jedis.scard("set"));
        System.out.println("s5是否存在set集合中：" + jedis.sismember("set", "s5"));
        System.out.println("s2是否存在set集合中：" + jedis.sismember("set", "s2"));

        System.out.println("==============向集合set(无序)中添加元素（不重复）==============");
        System.out.println(jedis.sadd("set1", "s0", "s1", "s2", "s3", "s4", "s5"));
        System.out.println(jedis.sadd("set2", "s00", "s10", "s20", "s30", "s1", "s2"));
        System.out.println("将set1中的s5删除并放入set3中："+jedis.smove("set1", "set3", "s5"));
        System.out.println("将set1中的s4删除并放入set3中："+jedis.smove("set1", "set3", "s4"));
        System.out.println("set1集合的所有元素：" + jedis.smembers("set1"));
        System.out.println("set3集合的所有元素：" + jedis.smembers("set3"));
        System.out.println("==============集合运算==============");
        System.out.println("set1集合的所有元素：" + jedis.smembers("set1"));
        System.out.println("set2集合的所有元素：" + jedis.smembers("set2"));
        System.out.println("set1、set2集合的交集：" + jedis.sinter("set1", "set2"));
        System.out.println("set1、set2集合的并集：" + jedis.sunion("set1", "set2"));
        System.out.println("set1、set2集合的差集：" + jedis.sdiff("set1", "set2"));

        System.out.println("set1、set2集合的交集并放到set4中：" + jedis.sinterstore("set4", "set1", "set2"));
        System.out.println("set4集合的所有元素：" + jedis.smembers("set4"));

        System.out.println("有顺序的集合 sortset：" + jedis.zadd("zset", 1, "s1"));
        System.out.println("有顺序的集合 sortset：" + jedis.zadd("zset", 3, "s0"));
        System.out.println(jedis.zrange("zset", 0, -1));
    }
}
